/* eslint-disable no-unused-vars,no-redeclare,handle-callback-err */
const { ObjectId } = require('mongodb')
const { DataBase } = require('./database')
// const Base = require('base')
const db = new DataBase()
db.setDatabase('content')
db.setCollection('gonggao')
// function printAll () {
//   db.findAll((result) => {
//     console.log(result)
//   })
// }

function insert () {
  db.insert('gonggao', { title: 'insert test' }, (doc, err) => {
    console.log(doc)
  })
}

function update () {
  db.updateOne('gonggao', { _id: ObjectId('5b68e38d2d6f0016280dd773') }, {
    _id: ObjectId('5b68e38d2d6f0016280dd773'),
    type: 'tongzhi',
    title: '888',
    'content': 'world177'
  }, (doc, err) => {
    console.log(doc)
  })
}

function find () {
  db.findOne('gonggao', { _id: ObjectId('5b68e38d2d6f0016280dd773') },
    (doc, err) => {
      console.log(doc)
    }
  )
}

function testFind () {
  let filter = {}
  let key = 'title'
  filter[key] = 'biaoti'
  db.findOne('content', filter,
    (doc, err) => {
      console.log(doc)
    }
  )
}

function testFind () {
  let filter = {}
  let key = 'title'
  filter[key] = 'biaoti'
  db.findOne('content', filter,
    (doc, err) => {
      console.log(doc)
    }
  )
}

function testRun () {
  db.run('gonggao', (collection) => {
    collection.find({ _id: ObjectId('5b68e38d2d6f0016280dd773') })
      .toArray((err, result) => {
        console.log(result)
      })
  })
}

function getAttribName () {
  let obj = { username: 'hby', password: 'huang' }
  let str = Obj2Url(obj)
  let url = 'http://localhost:3000/api/get/gonggao/'
  console.log(url + str)
}

/**
 * @return {string}
 */
function Obj2Url (obj) {
  let str = '?'
  for (let name in obj) {
    str += name + '=' + obj[name] + '&'
  }
  str = str.substr(0, str.length - 1)
  return str
}

function test () {
  // update()
  // find()
  // let o = ObjectId('5b68e38d2d6f0016280dd773')
  // let timestamp = o.getTimestamp()
  // console.log(timestamp)
  // update()
  // testRun()
  // testFind()
  getAttribName()
}

test()

// async function test () {
//   try {
//
//     await user.link()
//     console.log(user._id)
//     user.info2 = '6676'
//     db.save(user, () => {
//       printAll()
//     })
//   } catch (e) {
//     console.log('用户信息匹配错误')
//   }
// }
//
// test()
